package com.unicom.service.impl;

import com.unicom.common.ServerResponse;
import com.unicom.dao.UserMapper;
import com.unicom.pojo.User;
import com.unicom.service.IUserService;
import com.unicom.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("iUserService")
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public ServerResponse<User> login(String username, String password){
        //先查询用户名，看用户名是否存在
        int resultCount = userMapper.checkUsername(username);
        //如果查不到的话，用户不存在
        if (resultCount == 0) {
            return ServerResponse.createByErrorMessage("用户名不存在");
        }

        //密码登陆
        String md5Password = MD5Util.MD5EncodeUtf8(password);
        //System.out.println("+++++++++++"+md5Password+"++++++++++++");

        //通过用户名和密码进行查询
        User user = userMapper.selectLogin(username, password);
        if (user == null) {
            return ServerResponse.createByErrorMessage("密码错误");
        }
        //将密码设置为空
        user.setPassword(org.apache.commons.lang3.StringUtils.EMPTY);
        return ServerResponse.createBySuccess("登录成功", user);
    }
}
